package com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.controller;

import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.entity.CaseSelectionIntelligentScoreAssistanceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

/**
 * 智能评价
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/intelligent")
public class IntelliGenceController extends BaseController {
    @Autowired
    private SqlMapper sqlMapper;

    /**
     * 病例评选图片智能评价
     */
    @RequestMapping(value = "/evaluation", method = RequestMethod.POST)
    @ResponseBody
    public int addCustomerMarket(String imagePathPreoperative, String imagePathIntraoperative, String imagePathPostoperative,String id2) throws Exception {
        String command = "D:\\360Downloads\\war文件、\\pro\\teeth_score\\application.py";   // python程序的路径
        String urlId1=imagePathPreoperative.split(";")[0];
        String urlId2=imagePathIntraoperative.split(";")[0];
        String urlId3=imagePathPostoperative.split(";")[0];
        String selsql1="select id,image_path from case_selection_intelligent_score_assistance_info where id="+urlId1+"";
        String selsql2="select id,image_path from case_selection_intelligent_score_assistance_info where id="+urlId2+"";
        String selsql3="select id,image_path from case_selection_intelligent_score_assistance_info where id="+urlId3+"";
        CaseSelectionIntelligentScoreAssistanceInfo info1=sqlMapper.selectOne(selsql1,CaseSelectionIntelligentScoreAssistanceInfo.class);
        CaseSelectionIntelligentScoreAssistanceInfo info2=sqlMapper.selectOne(selsql2,CaseSelectionIntelligentScoreAssistanceInfo.class);
        CaseSelectionIntelligentScoreAssistanceInfo info3=sqlMapper.selectOne(selsql3,CaseSelectionIntelligentScoreAssistanceInfo.class);
        String url1=info1.getImagePath().replace("http://47.98.150.186:8070","D:/201903071/apache-tomcat-8.0.30/webapps/ROOT");
        String url2=info2.getImagePath().replace("http://47.98.150.186:8070","D:/201903071/apache-tomcat-8.0.30/webapps/ROOT");
        String url3=info3.getImagePath().replace("http://47.98.150.186:8070","D:/201903071/apache-tomcat-8.0.30/webapps/ROOT");
        String coordinatea1=imagePathPreoperative.split(";")[2];
        String coordinatea2=imagePathPreoperative.split(";")[3];
        String coordinateb1=imagePathIntraoperative.split(";")[2];
        String coordinateb2=imagePathIntraoperative.split(";")[3];
        String coordinatec1=imagePathPostoperative.split(";")[2];
        String coordinatec2=imagePathPostoperative.split(";")[3];
        String lengthtea1 = coordinatea1.split(",")[0];
        String lengthtea2 = coordinatea1.split(",")[1];
        String lengthtea3 = coordinatea2.split(",")[0];
        String lengthtea4 = coordinatea2.split(",")[1];
        String lengthteb1 = coordinateb1.split(",")[0];
        String lengthteb2 = coordinateb1.split(",")[1];
        String lengthteb3 = coordinateb2.split(",")[0];
        String lengthteb4 = coordinateb2.split(",")[1];
        String lengthtec1 = coordinatec1.split(",")[0];
        String lengthtec2 = coordinatec1.split(",")[1];
        String lengthtec3 = coordinatec2.split(",")[0];
        String lengthtec4 = coordinatec2.split(",")[1];
        String coordinateaa1 = lengthtea2+","+lengthtea1;
        String coordinateaa2 = lengthtea4+","+lengthtea3;
        String coordinateab1 = lengthteb2+","+lengthteb1;
        String coordinateab2 = lengthteb4+","+lengthteb3;
        String coordinateac1 = lengthtec2+","+lengthtec1;
        String coordinateac2 = lengthtec4+","+lengthtec3;
        String urlStr=url1+","+url2+","+url3;
        String coordinateStr=coordinateaa1+","+coordinateaa2+","+coordinateab1+","+coordinateab2+","+coordinateac1+","+coordinateac2;
        String[] cmd = new String[]{"python", command,urlStr,coordinateStr};
        //getCmdOutput(cmd);  // 通过Runtime.getRuntime()执行脚本文件

        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        try {
            process = runtime.exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String charsetName = "GB2312";
        InputStreamReader reader;
        try {
            //获取标准的输出
            reader = new InputStreamReader(process.getInputStream(), charsetName);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return 1;
        }
        BufferedReader bufferedReader = new BufferedReader(reader);
        String line;
        String returnString = null;
        try {
            while (true) {
                line = bufferedReader.readLine();
                //赋值第一行给返回变量
                if(returnString == null){
                    returnString = line;
                }
                if (line == null)
                    break;
                System.out.println(line);
            }
            process.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String lineStr[]=returnString.split("#");
        for(int i = 1; i<= lineStr.length; i++){
            String curStr[] = lineStr[i].split("\\s");
            String sql = "insert into intelligent_score_details_info (photo_type,A1,A2,A3,A4,A5,A6,A7,score,create_time) VALUES ('"+i+"','" + curStr[1] + "','" + curStr[2] + "','" + curStr[3] + "'," +
                    "'" + curStr[4] + "','" + curStr[5] + "','" + curStr[6] + "','" + curStr[7] + "','"+curStr[8]+"','" + DateTimeKit.now() + "')";
            sqlMapper.insert(sql);
        }
        return 1;
    }

//    public static void main(String[] args) throws IOException {
//        String command = "D:\\Python\\python3.6.5\\Lib\\site-packages\\tensorboard\\backend\\application.py";   // python程序的路径
//        String[] cmd = new String[]{"python", command};
//        getCmdOutput(cmd);  // 通过Runtime.getRuntime()执行脚本文件
//        System.out.println();
//    }



//    public  String getCmdOutput(String[] cmd) {
//        //JAVA实现实时Process，实时获取输出
//        Runtime runtime = Runtime.getRuntime();
//        Process process = null;
//        try {
//            process = runtime.exec(cmd);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        String charsetName = "GB2312";
//        InputStreamReader reader;
//        try {
//            //获取标准的输出
//            reader = new InputStreamReader(process.getInputStream(), charsetName);
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//            return null;
//        }
//        BufferedReader bufferedReader = new BufferedReader(reader);
//        String line;
//        String returnString = null;
//        try {
//            while (true) {
//                line = bufferedReader.readLine();
//                //赋值第一行给返回变量
//                if(returnString == null){
//                    returnString = line;
//                }
//                if (line == null)
//                    break;
//                System.out.println(line);
//            }
//            process.waitFor();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        String lineStr[]=returnString.split("#");
//        for(int i = 1; i<= lineStr.length; i++){
//            String curStr[] = lineStr[i].split("\\s");
//            String sql = "insert into intelligent_score_details_info (photo_type,A1,A2,A3,A4,A5,A6,A7,score,create_time) VALUES ('"+i+"','" + curStr[1] + "','" + curStr[2] + "','" + curStr[3] + "'," +
//                    "'" + curStr[4] + "','" + curStr[5] + "','" + curStr[6] + "','" + curStr[7] + "','"+curStr[8]+"','" + DateTimeKit.now() + "')";
//            sqlMapper.insert(sql);
//        }
//        return returnString;
//    }

    //提交保存
    @RequestMapping(value = "/evaluations", method = RequestMethod.POST)
    @ResponseBody
    public  String getCmdOutputs(String[] cmd,String imagePathPreoperative, String imagePathIntraoperative, String imagePathPostoperative,String id2) {
        //JAVA实现实时Process，实时获取输出
        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        try {
            process = runtime.exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String charsetName = "GB2312";
        InputStreamReader reader;
        try {
            //获取标准的输出
            reader = new InputStreamReader(process.getInputStream(), charsetName);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
        BufferedReader bufferedReader = new BufferedReader(reader);
        String line;
        String returnString = null;
        try {
            while (true) {
                line = bufferedReader.readLine();
                //赋值第一行给返回变量
                if(returnString == null){
                    returnString = line;
                }
                if (line == null)
                    break;
                System.out.println(line);
            }
            process.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String lineStr[]=returnString.split("#");
        for(int i = 1; i<= lineStr.length; i++){
            String curStr[] = lineStr[i].split("\\s");
            String sql = "insert into intelligent_score_details_info (photo_type,A1,A2,A3,A4,A5,A6,A7,score,create_time) VALUES ('"+i+"','" + curStr[1] + "','" + curStr[2] + "','" + curStr[3] + "'," +
                    "'" + curStr[4] + "','" + curStr[5] + "','" + curStr[6] + "','" + curStr[7] + "','"+curStr[8]+"','" + DateTimeKit.now() + "')";
            sqlMapper.insert(sql);
        }
        return returnString;
    }
}